package model

import (
	"go.uber.org/zap"
	"goskeleton/app/global/variable"
)

type Chat struct {
	BaseModel
	ConversationId string `json:"conversation_id"`
	SenderOpenId   string `json:"sender_open_id"`
	ReceiverOpenId string `json:"receiver_open_id"`
	MessageType    string `json:"message_type"`
	Content        string `json:"content"`
	Status         int    `json:"status"`
}

type ChatRecord struct {
	BaseModel
	ConversationId    string `json:"conversation_id"`
	SenderOpenId      string `json:"sender_open_id"`
	ReceiverOpenId    string `json:"receiver_open_id"`
	SenderAvatarUrl   string `json:"sender_avatar_url"`
	ReceiverAvatarUrl string `json:"receiver_avatar_url"`
}

func (Chat) TableName() string {
	return "tb_chat"
}
func (ChatRecord) TableName() string {
	return "tb_chat_record"
}

func CreateChatFactory1(sqlType string) *Chat {
	return &Chat{BaseModel: BaseModel{DB: UseDbConn(sqlType)}}
}
func CreateChatFactory2(sqlType string) *ChatRecord {
	return &ChatRecord{BaseModel: BaseModel{DB: UseDbConn(sqlType)}}
}

/*
	func (cr *ChatRecord) CreateChatRecord(senderOpenId, receiverOpenId string) {
		//查看聊天框记录里是否有二人之间的聊天
		if cr.GetChatRecord(senderOpenId, receiverOpenId) == nil {
			cr.DB.Create(&ChatRecord{
				ConversationId: senderOpenId + "" + receiverOpenId,
				SenderOpenId:   senderOpenId,
				ReceiverOpenId: receiverOpenId,
			})
		}
	}
*/
func (cr *ChatRecord) CreateChatRecord(senderOpenId, receiverOpenId string) {
	// 查看聊天框记录里是否有二人之间的聊天
	if cr.GetChatRecord(senderOpenId, receiverOpenId) == nil {
		// 获取发送者和接收者的头像URL
		senderAvatar := GetUserAvatar(senderOpenId)
		receiverAvatar := GetUserAvatar(receiverOpenId)

		cr.DB.Create(&ChatRecord{
			ConversationId:    senderOpenId + "" + receiverOpenId,
			SenderOpenId:      senderOpenId,
			ReceiverOpenId:    receiverOpenId,
			SenderAvatarUrl:   senderAvatar,
			ReceiverAvatarUrl: receiverAvatar,
		})
	}
}

func (cr *ChatRecord) GetChatRecord(senderOpenId, receiverOpenId string) *ChatRecord {
	var chatRecord ChatRecord
	result1 := cr.DB.Where("sender_open_id = ? AND receiver_open_id = ?", senderOpenId, receiverOpenId).Find(&chatRecord)
	result2 := cr.DB.Where("sender_open_id = ? AND receiver_open_id = ?", receiverOpenId, senderOpenId).Find(&chatRecord)
	if result1.Error != nil && result2.Error != nil {
		variable.ZapLog.Error("查询聊天记录时出错", zap.Error(result1.Error))
		return nil
	}
	if result1.RowsAffected == 0 && result2.RowsAffected == 0 {
		variable.ZapLog.Info("记录未找到", zap.String("sender_open_id", senderOpenId), zap.String("receiver_open_id", receiverOpenId))
		return nil
	}
	variable.ZapLog.Info("记录找到", zap.Any("chat_record", chatRecord))
	return &chatRecord
}

func (c *Chat) CreateChat(senderOpenId, receiverOpenId, content string, status int) {
	c.DB.Create(&Chat{
		ConversationId: senderOpenId + "" + receiverOpenId,
		SenderOpenId:   senderOpenId,
		ReceiverOpenId: receiverOpenId,
		Status:         status,
		Content:        content,
	})
}

// 获取用户间的聊天记录
func (c Chat) GetChatMessage(selfOpenId string, targetUserOpenId string) []Chat {

	var chatMessage []Chat
	//查找对应的聊天信息，可能分别是发送者或者接收者，把两者之间都查询出来
	c.DB.Where("sender_open_id = ? AND receiver_open_id = ?", selfOpenId, targetUserOpenId).Or("sender_open_id = ? AND receiver_open_id = ?", targetUserOpenId, selfOpenId).Find(&chatMessage)
	//打印出获取到的信息
	variable.ZapLog.Info("获取聊天记录", zap.Any("chat_message", chatMessage))
	return chatMessage
}

// 查找相关的聊天记录,本身有可能是发送者或者接收者
func (c *ChatRecord) GetChatRecords(OpenId string) []ChatRecord {
	var chatsRecord []ChatRecord
	c.DB.Where("sender_open_id = ? OR receiver_open_id = ?", OpenId, OpenId).Find(&chatsRecord)
	return chatsRecord
}
